/*
Copyright To change this template, choose Tools | Templates
Copyright and open the template in the editor.
 */
package com.click.app.model;

import com.click.app.Functions;
import com.click.app.util.Utilities;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * @author Joel Chuca Condo
 * @date 07/04/2009
 */
@Entity
@Table(name = "purchases")
public class Purchase extends BaseObject {

    private Long id;
    private Bill bill;
    private Coin coin;
    private Supplier supplier;
    private User user;
    private Date date;
    private Date timeCredit;
    private Double totalPrice;
    private Double restPrice;
    private Boolean state;
    private Double discount;
    private Double discountXfail;
    private List<PurchasePayment> purchasePayments = new ArrayList<PurchasePayment>();
    private List<PurchaseDetail> purchaseDetails = new ArrayList<PurchaseDetail>();
    private Boolean deleted;

    public Purchase() {
        date = new Date();
        totalPrice = new Double(0);
        restPrice = new Double(0);
        discount = new Double(0);
        state = true;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id_purchase")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "id_bill")
    public Bill getBill() {
        return bill;
    }

    public void setBill(Bill Bill) {
        this.bill = Bill;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_coin")
    public Coin getCoin() {
        return this.coin;
    }

    public void setCoin(Coin coin) {
        this.coin = coin;

    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_supplier", nullable = false)
    public Supplier getSupplier() {
        return this.supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_user", nullable = false)
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "date", length = 10)
    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "time_credit", length = 10)
    public Date getTimeCredit() {
        return this.timeCredit;
    }

    public void setTimeCredit(Date timeCredit) {
        this.timeCredit = timeCredit;
    }

    @Column(name = "price_total", precision = 12, scale = 0, columnDefinition = "real default 0")
    public Double getTotalPrice() {
        return Functions.getDouble3Decimal(totalPrice);
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = Functions.getDouble3Decimal(totalPrice);
    }

    @Column(name = "price_rest", precision = 12, scale = 0)
    public Double getRestPrice() {
        return Functions.getDouble3Decimal(restPrice);
    }

    public void setRestPrice(Double restPrice) {
        this.restPrice = Functions.getDouble3Decimal(restPrice);
    }

    @Column(name = "state")
    public Boolean getState() {
        return this.state;
    }

    public void setState(Boolean state) {
        this.state = state;
    }

    @Column(name = "discount")
    public Double getDiscount() {
        return Functions.getDouble3Decimal(discount);
    }

    public void setDiscount(Double discount) {
        this.discount = Functions.getDouble3Decimal(discount);
    }

    @Column(name = "discount_x_fail")
    public Double getDiscountXfail() {
        return Functions.getDouble3Decimal(discountXfail);
    }

    public void setDiscountXfail(Double discountXfail) {
        this.discountXfail = Functions.getDouble3Decimal(discountXfail);
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchase")
    public List<PurchasePayment> getPurchasePayments() {
        return purchasePayments;
    }

    public void setPurchasePayments(List<PurchasePayment> purchasePayments) {
        this.purchasePayments = purchasePayments;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchase")
    public List<PurchaseDetail> getPurchaseDetails() {
        if (purchaseDetails == null) {
            purchaseDetails = new ArrayList<PurchaseDetail>();
        }
        return this.purchaseDetails;
    }

    public void setPurchaseDetails(List<PurchaseDetail> purchaseDetailses) {
        this.purchaseDetails = purchaseDetailses;
    }

    @Transient
    public Double getRealPrice() {
        return totalPrice - discount - discountXfail;
    }

    @Column(name = "deleted")
    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    public void updateDeleted(boolean val) {
        for (Iterator<PurchaseDetail> it = purchaseDetails.iterator(); it.hasNext();) {
            PurchaseDetail purchDetail = it.next();
            purchDetail.setDeleted(val);
        }
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Purchase)) {
            return false;
        }
        Purchase other = (Purchase) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(this.getId()).append(this.totalPrice).append(this.restPrice).append(this.state).append(this.discount).append(this.restPrice).append(this.discountXfail).toString();
    }

    @Transient
    public Double getDetailsTotal() {
        Double total = 0.0D;
        for (PurchaseDetail pd : getPurchaseDetails()) {
            total += (pd.getTotalPrice());
        }
        return total;
    }

    @Transient
    public String getDateFormatted() {
        return Utilities.convertDate(date);
    }
}
